package com.yaozheng.vocationaltraining.service.impl.checkpoints_trip;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.IBaseView;
import com.yaozheng.vocationaltraining.iview.IOnlineExamView;
import com.yaozheng.vocationaltraining.iview.checkpoints_trip.ICheckpointsTripQuestionView;
import com.yaozheng.vocationaltraining.service.OnlineExamService;
import com.yaozheng.vocationaltraining.service.checkpoints_trip.CheckpointsTripQuestionService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EBean;

import java.util.HashMap;

/**
 * 闯关提交答案
 */
@EBean
public class CheckpointsTripSubmitAnswersServiceImpl implements OnlineExamService, ICheckpointsTripQuestionView {
    IOnlineExamView iOnlineExamView;
    @Bean(CheckpointsTripQuestionServiceImpl.class)
    CheckpointsTripQuestionService checkpointsTripQuestionService;

    @Override
    public void init(IOnlineExamView iOnlineExamView) {
        this.iOnlineExamView = iOnlineExamView;
    }

    @Override
    public void getQuestionList(int paperId) {
        checkpointsTripQuestionService.getQuestionList(paperId);
    }


    @Background
    @Override
    public void submitAnswers(int paperId, String answers) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", paperId);
            jsonObject.put("answers", answers);
            OkHttpClientManager.getInstance().postJson(
                    Constants.ROOTURL + "/exam/advanced",
                    iOnlineExamView.getToken(), jsonObject.toString(),
                    new OkHttpClientManager.IBaseViewCallback(new BaseView(iOnlineExamView) {

                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iOnlineExamView.submitAnswersSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iOnlineExamView.submitAnswersError(error);
                        }


                    }));
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iOnlineExamView.isResponseResult()) {
                iOnlineExamView
                        .submitAnswersError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

    @Override
    public void loadSaiKuangInfo(int gameId) {

    }

    @Override
    public void getQuestionListSuccess(JSONObject jsonObject) {
        iOnlineExamView.responseSuccess(jsonObject);
    }

    @Override
    public void getQuestionListError(String error) {
        iOnlineExamView.responseError(error);
    }

    @Override
    public String getToken() {
        return iOnlineExamView.getToken();
    }

    @Override
    public boolean isResponseResult() {
        return iOnlineExamView.isResponseResult();
    }

    @Override
    public void tokenFail() {
        iOnlineExamView.tokenFail();
    }
}
